package com.web.exiaodao.business.service.impl;

import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.XaUtil;
import com.web.exiaodao.business.entity.ExchangeProducts;
import com.web.exiaodao.business.repository.ExchangeProductsRepository;
import com.web.exiaodao.business.service.ExchangeProductsService;

@Service("ExchangeProductsService")
@Transactional(readOnly = false)
public class ExchangeProductsServiceImpl extends BaseService<ExchangeProducts> implements ExchangeProductsService {

	private static final Logger LOGGER = Logger.getLogger(ExchangeProductsServiceImpl.class);

	@Autowired
	private ExchangeProductsRepository exchangeProductsRepository;
	
	/**
	 * 查询单条ExchangeProducts信息
	 * //@param tId
	 * @return 返回单个ExchangeProducts对象
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<ExchangeProducts> findOne(Long modelId) throws BusinessException {
		ExchangeProducts obj = new ExchangeProducts();
		if(modelId != 0){
			obj = exchangeProductsRepository.findByIdAndStatusNot(modelId,XaConstant.Status.delete);
		}
		XaResult<ExchangeProducts> xr = new XaResult<ExchangeProducts>();
		if (XaUtil.isNotEmpty(obj)) {
			xr.setObject(obj);
			LOGGER.info("后台----->查询单个实体----->ExchangeProducts");
		} else {
			xr.error(XaConstant.Message.object_not_find);
			LOGGER.info(XaConstant.Message.object_not_find);
		}
		return xr;
	}

	/**
	 * 分页查询状态非status的ExchangeProducts数据
	 * @param status
	 * @param filterParams
	 * @param pageable
	 * @return 返回对象ExchangeProducts集合
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<Page<ExchangeProducts>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable) throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示非删除的所有数据
			status = XaConstant.Status.delete;
		}
		filters.put("status", new SearchFilter("status", Operator.NE, status));
		Page<ExchangeProducts> page = exchangeProductsRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ExchangeProducts.class), pageable);
		XaResult<Page<ExchangeProducts>> xr = new XaResult<Page<ExchangeProducts>>();
		xr.setObject(page);
		LOGGER.info("后台----->分页查询实体集合（状态不等于）----->ExchangeProducts");
		return xr;
	}

	/**
	 * 分页查询状态status的ExchangeProducts数据
	 * @param status
	 * @param filterParams
	 * @param pageable
	 * @return 返回对象ExchangeProducts集合
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<Page<ExchangeProducts>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable) throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示正常数据
			status = XaConstant.Status.valid;
		}
		filters.put("status", new SearchFilter("status", Operator.EQ, status));
		Page<ExchangeProducts> page = exchangeProductsRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ExchangeProducts.class), pageable);
		XaResult<Page<ExchangeProducts>> xr = new XaResult<Page<ExchangeProducts>>();
		xr.setObject(page);
		LOGGER.info("后台----->分页查询实体集合（状态等于）----->ExchangeProducts");
		return xr;
	}

	/**
	 * 保存ExchangeProducts信息
	 * @param model
	 * @return
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor = Exception.class)
	public XaResult<ExchangeProducts> saveOrUpdate(ExchangeProducts model) throws BusinessException {
		ExchangeProducts obj = null;
		if(XaUtil.isNotEmpty(model.getId())){
			obj = exchangeProductsRepository.findOne(model.getId());
		}else{
			obj = new ExchangeProducts();
		}
	    obj.setProductName(XaUtil.isNotEmpty(model.getProductName()) ? model.getProductName() : obj.getProductName());
	    obj.setNeedCoins(XaUtil.isNotEmpty(model.getNeedCoins()) ? model.getNeedCoins() : obj.getNeedCoins());
	    obj.setProductImg(XaUtil.isNotEmpty(model.getProductImg()) ? model.getProductImg() : obj.getProductImg());
	    obj.setInventory(XaUtil.isNotEmpty(model.getInventory()) ? model.getInventory() : obj.getInventory());
	    obj.setExchangedNum(XaUtil.isNotEmpty(model.getExchangedNum()) ? model.getExchangedNum() : obj.getExchangedNum());
	    obj.setProductIntro(XaUtil.isNotEmpty(model.getProductIntro()) ? model.getProductIntro() : obj.getProductIntro());
	    obj.setProductProvider(XaUtil.isNotEmpty(model.getProductProvider()) ? model.getProductProvider() : obj.getProductProvider());
		obj = exchangeProductsRepository.save(obj);
		XaResult<ExchangeProducts> xr = new XaResult<ExchangeProducts>();
		xr.setObject(obj);
		LOGGER.info("后台新增或编辑数据成功！");
		return xr;
	}

	/**
	 * 修改ExchangeProducts状态，可一次修改多条   3删除  -1锁定  1正常
	 * //@param userId
	 * @param modelIds
	 * @param status
	 * @return 返回ExchangeProducts对象
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor = Exception.class)
	public XaResult<ExchangeProducts> multiOperate(
			String modelIds,Integer status) throws BusinessException {
		XaResult<ExchangeProducts> xr = new XaResult<ExchangeProducts>();
		if(status == null){
			status = XaConstant.Status.delete;
		}
		if(modelIds != null){
			String[] ids = modelIds.split(",");
			for(String id : ids){
				ExchangeProducts obj = exchangeProductsRepository.findByIdAndStatusNot(Long.parseLong(id),status);
				if (XaUtil.isNotEmpty(obj)) {
					obj.setStatus(status);
					obj = exchangeProductsRepository.save(obj);
				}
			}
		}
		LOGGER.info("批量修改数据状态成功！");
		return xr;
	}
	
}
